package com.youlai.boot.modules.banner.converter;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;

import com.youlai.boot.modules.banner.model.Banner;
import com.youlai.boot.modules.banner.model.BannerForm;
import com.youlai.boot.modules.banner.model.BannerVO;
import com.youlai.boot.modules.area.service.AreaService;
import com.youlai.boot.modules.merchant.service.MerchantService;

@Mapper(componentModel = "spring")
public abstract class BannerConverter {

    @Autowired
    protected AreaService areaService;

    @Autowired
    protected MerchantService merchantService;

    @Mapping(target = "linkName", source = ".", qualifiedByName = "getLinkName")
    public abstract BannerVO toVO(Banner banner);

    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    public abstract Banner toEntity(BannerForm form);

    public abstract BannerForm toForm(Banner banner);

    @Named("getLinkName")
    protected String getLinkName(Banner banner) {
        if (banner.getLinkId() == null) {
            return null;
        }

        return switch (banner.getLinkType()) {
            case 1 -> areaService.getById(banner.getLinkId()).getName();
            case 2 -> merchantService.getById(banner.getLinkId()).getName();
            case 3 -> "展会 #" + banner.getLinkId(); // 展会服务未实现
            case 4 -> "通知 #" + banner.getLinkId(); // 通知服务未实现
            default -> null;
        };
    }
}
